package crawler.douban;

import constant.DBConstant;
import crawler.http.HttpClientTest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 豆瓣电影列表爬取
 * USER: lintc 【lintiancong@zhuojianchina.com】
 * DATE: 2017-09-19 14:27
 */
public class DBMovieListCatch implements Runnable {

    int type;

    int level = 100;

    int num = 0;

    int limit = 20;

    public DBMovieListCatch(int type) {
        this.type = type;
    }

    @Override
    public void run() {
        while (level != 0) {
            String url = DBConstant.DB_MOVIE_LIST_API.replace("MOVIE_TYPE", type + "")
                    .replace("LEVEL_START", level + "")
                    .replace("LEVEL_END", (level - 10) + "")
                    .replace("START_NUM", num + "")
                    .replace("LIMIT_NUM", limit + "");
            String str = HttpClientTest.get(url);
            JSONArray ary = JSONArray.fromObject(str);
            if (ary.size() == 0) { // 当前等级已没有
                level = level - 10;
                num = 0;
                if (type != 30 && level == 0) {
                    type = type + 1;
                    level = 100;
                }
            } else {
                for (Object o : ary) {
                    JSONObject obj = (JSONObject) o;
                    System.out.println(obj);
                    Movie movie = new Movie(obj);
                    DouBanSpider.setWaitList(movie);
                }
                num = num + limit;
            }
            System.out.println(url + ": (等级) " + level + ": (获取)" + ary.size() + ": (待抓取)" + DouBanSpider.waitList.size());
            DouBanSpider.friendlyToDouban(10);
        }
    }

    public static void main(String[] args) {
        DBMovieListCatch listCatch = new DBMovieListCatch(1);
        new Thread(listCatch).start();
    }
}
